{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fdc92674",
   "metadata": {},
   "source": [
    "----\n",
    "- 2022/10/11 /week08\n",
    "- Python 课堂笔记\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdeeb5ac",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ec74801",
   "metadata": {},
   "source": [
    "> 0. 代码的注释\n",
    "> 1. 用户输入\n",
    "> 2. 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c88d46c",
   "metadata": {},
   "source": [
    "# 0.代码的注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3cc0611",
   "metadata": {},
   "source": [
    "- 1.一般用# 号键，写在代码的最上边，或者每行代码的最后，用于记录代码的思路"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d6b54f8",
   "metadata": {},
   "source": [
    "- 2.'''注释内容'''，用三引号，一般是注释代码块，内容不会运行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "891dc1fa",
   "metadata": {},
   "source": [
    "# 1.用户输入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9896eeed",
   "metadata": {},
   "source": [
    "> 1.input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d0ffb42e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "admin\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登录系统\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5796a74e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:12345678\n",
      "账号不存在或密码不正确\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了判断功能(联想到if)\n",
    "# 假设我们数据库有用户信息：账号：admin，密码：123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景一：输入信息正确，登录成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录成功\")\n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "86e08bae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:fsgeg\n",
      "密码:geffqeg\n",
      "账号不存在或密码不正确!\n",
      "您还有2次尝试的机会\n",
      "账号:cergewg\n",
      "密码:东侧rfwg\n",
      "账号不存在或密码不正确!\n",
      "您还有1次尝试的机会\n",
      "账号:qdwqg\n",
      "密码:的份hth\n",
      "三次输入失败，请1小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代二：增加循环功能(联想到for循环)\n",
    "# 假设我们数据库有用户信息：账号：admin，密码，123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 是一个 int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    # 情景一：输入信息正确，登陆成功\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+user1+\"登录！\")\n",
    "        break # 终止循环\n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确!\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb8a22f4",
   "metadata": {},
   "source": [
    "# 2.格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3668644c",
   "metadata": {},
   "source": [
    "> 1. 字符串拼接\n",
    "> 2. 占位符\n",
    "> 3. format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "17642dcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：温欣欣\n",
      "年龄：20\n",
      "工作：程序猿\n",
      "收入：30000\n",
      "地区：珠海\n",
      "-----INFO OF 温欣欣------\n",
      "年龄：20\n",
      "工作：程序猿\n",
      "收入：30000\n",
      "地区：珠海\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b8b0a0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄:20\n",
      "20 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，都以字符串形式返回\n",
    "age = input(\"年龄:\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f5727221",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：jenny\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n",
      "-----INFO OF jenny------\n",
      "姓名：jenny\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s str, 依次有序输入\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF %s------\n",
    "姓名：%s\n",
    "年龄：%d\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s'''%(name,name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2a104ba5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：温欣欣\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n",
      "-----INFO OF 温欣欣------\n",
      "姓名：温欣欣\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format() 格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary, 地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd085072",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0480e78b",
   "metadata": {},
   "source": [
    "# 课后练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d98baf7a",
   "metadata": {},
   "source": [
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "> 2. 让用户输入商品编号进行商品的购买。\n",
    "> 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "> 4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb9ba978",
   "metadata": {},
   "source": [
    "# Python 购物车"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cb7ad418",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入你的工资5000\n",
      "0 ('iphone 14', 5800)\n",
      "1 ('makeup', 500)\n",
      "2 ('vivo', 2000)\n",
      "3 ('clothes', 600)\n",
      "选择要买的商品2\n",
      "add vivo into shopping car,you current salary 3000\n",
      "0 ('iphone 14', 5800)\n",
      "1 ('makeup', 500)\n",
      "2 ('vivo', 2000)\n",
      "3 ('clothes', 600)\n",
      "选择要买的商品1\n",
      "add makeup into shopping car,you current salary 2500\n",
      "0 ('iphone 14', 5800)\n",
      "1 ('makeup', 500)\n",
      "2 ('vivo', 2000)\n",
      "3 ('clothes', 600)\n",
      "选择要买的商品3\n",
      "add clothes into shopping car,you current salary 1900\n",
      "0 ('iphone 14', 5800)\n",
      "1 ('makeup', 500)\n",
      "2 ('vivo', 2000)\n",
      "3 ('clothes', 600)\n",
      "选择要买的商品0\n",
      "\u001b[41;1m你的余额只剩[1900]了，不能买了,按q退出\u001b[0m\n",
      "0 ('iphone 14', 5800)\n",
      "1 ('makeup', 500)\n",
      "2 ('vivo', 2000)\n",
      "3 ('clothes', 600)\n",
      "选择要买的商品q\n",
      "购物车的商品如下所示\n",
      "('vivo', 2000)\n",
      "('makeup', 500)\n",
      "('clothes', 600)\n",
      "余额 1900\n"
     ]
    }
   ],
   "source": [
    "#定义一个列表存放商品信息\n",
    "products=[('iphone 14',5800),('makeup',500),('vivo',2000),('clothes',600)]\n",
    "shopping_list=[]#将购买的商品存在列表shopping_list 中\n",
    "salary=input(\"输入你的工资\")\n",
    "#判断输入的工资是否是数字\n",
    "if salary.isdigit():\n",
    "    salary=int(salary)#把输入的工资转换为整型\n",
    "    while True:\n",
    "        # for i in products:输出商品编号和商品信息两种方式均可\n",
    "        #     print(products.index(i),i)\n",
    "        for index,i in enumerate(products):\n",
    "            print(index,i)\n",
    "        user_choice=input(\"选择要买的商品\")\n",
    "        #通过输入商品编号来选择商品\n",
    "        if user_choice.isdigit():\n",
    "            user_choice=int(user_choice)\n",
    "            if user_choice>=0 and user_choice<len(products):\n",
    "                p_item=products[user_choice]\n",
    "                if p_item[1]<=salary:\n",
    "                    shopping_list.append(p_item)#把买的东西放到购物车\n",
    "                    salary-=p_item[1]#计算买完东西以后剩下的钱\n",
    "                    print(\"add %s into shopping car,you current salary %s\" %(p_item[0],salary))\n",
    "                else:\n",
    "                    print(\"\\033[41;1m你的余额只剩[%s]了，不能买了,按q退出\\033[0m\" %salary)\n",
    "            else:\n",
    "                print(\"商品不存在\")\n",
    "        elif user_choice=='q':\n",
    "            print(\"购物车的商品如下所示\")\n",
    "            for p in shopping_list:\n",
    "                print(p)\n",
    "            print(\"余额\",salary)\n",
    "            break\n",
    "        else:\n",
    "            print(\"invalid input\")\n",
    "else:\n",
    "    print(\"invalid input\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "433d44ee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb29890b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "213e7548",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56f9a93e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08c8f5e6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e518c248",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c8718ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff353a30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ed3673a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "153d9117",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef061acb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff4f6bae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25bc8a52",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4663726",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ee79e05",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af9f5cd0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d3d50f0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b71d1596",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c13df531",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60ce2a46",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
